<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <style>
            .error {color: #FF0000;}
        </style>
        <meta charset="UTF-8">
        <title>PHP测试</title>
    </head>
    <body>
        <h2>PHP 验证实例</h2>  
        <p><span class="error">* 必需的字段</span></p>
        <?php
            $name = $email = $gender = $comment = $website = "";
            $nameErr = $emailErr = $genderErr = $websiteErr = "";
            
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                if(empty($_POST['name'])){
                        $nameErr = "Name is required";
                    }  else {
                        $name = test_input($_POST["name"]);
                        if(!preg_match("/^[a-zA-Z ]*$/", $name)){
                            $nameErr = "只允许字母和空格！"; 
                        }                                
                    }
                
                 if (empty($_POST["email"])) {
                         $emailErr = "Email is required";
                    } else {
                        $email = test_input($_POST["email"]);
                        if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
                            $emailErr = "无效的 email 格式！"; 
                        }
                    }

                    if (empty($_POST["website"])) {
                        $website = "";
                    } else {
                        $website = test_input($_POST["website"]);
                        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%
                        =~_|]/i",$website)) {
                            $websiteErr = "无效的 URL"; 
                        }
                    }

                    if (empty($_POST["comment"])) {
                        $comment = "";
                    } else {
                        $comment = test_input($_POST["comment"]);
                    }

                    if (empty($_POST["gender"])) {
                        $genderErr = "Gender is required";
                    } else {
                        $gender = test_input($_POST["gender"]);
                    }
            }
            
            function test_input($data) {
                $data = trim($data);
                $data = stripslashes($data);
                $data = htmlspecialchars($data);
                return $data;
            }
        ?>
        
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
            Name: <input type="text" name="name" value="<?php echo $name;?>">
            <span class="error">* <?php echo $nameErr;?></span><br><br>
            E-mail: <input type="text" name="email" value="<?php echo $email;?>">
            <span class="error">* <?php echo $emailErr;?></span><br><br>
            Website:<input type="text" name="website" value="<?php echo $website;?>">
            <span class="error"><?php echo $websiteErr;?></span><br><br>
            <label>Comment:<textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><br>
            <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
            <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
            <span class="error">* <?php echo $genderErr;?></span><br><br>
            <input type="submit">
        </form>
        
        <?php
            echo "<h2>您的输入：</h2>";
            echo $name;
            echo "<br>";
            echo $email;
            echo "<br>";
            echo $website;
            echo "<br>";
            echo $comment;
            echo "<br>";
            echo $gender;
        ?>
        <?php
            $cars = array
            (
                array("Volvo",22,18),
                array("BMW",15,13),
                array("Saab",5,2),
                array("Land Rover",17,15)
            );
            
            for ($row = 0; $row < 4; $row++) {
                echo "<p><b>Row number $row</b></p>";
                echo "<ul>";
                for ($col = 0; $col < 3; $col++) {
                echo "<li>".$cars[$row][$col]."</li>";
                }
            echo "</ul>";
            }
        ?>
        
    </body>
</html>